package com.joekai.view_demo.dot_loading;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

import com.joekai.view_demo.utils.PxUtils;

/**
 * @author joekai
 */
public class DotLoadingView extends RelativeLayout {
    private final DotView mLeftView;
    private final DotView mMiddleView;
    private final DotView mRightView;
    //切换距离
    private int mTranslationDistance = 40;
    //动画时长
    private static final long ANIMATION_TIME = 250;
    // 是否停止动画
    private boolean mIsStopAnimator = false;

    private static final String TRANSLATION_X = "translationX";

    public DotLoadingView(Context context) {
        this(context, null);
    }

    public DotLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DotLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public DotLoadingView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mTranslationDistance = PxUtils.dp2px(mTranslationDistance);

        setBackgroundColor(Color.WHITE);
        // 添加三个View 但是要圆形
        mLeftView = getDotView(context);
        mLeftView.exchangeColor(Color.BLUE);
        mMiddleView = getDotView(context);
        mMiddleView.exchangeColor(Color.RED);
        mRightView = getDotView(context);
        mRightView.exchangeColor(Color.GREEN);
        addView(mLeftView);
        addView(mRightView);
        addView(mMiddleView);
        post(this::expendAnimation);
    }

    /**
     * 展开动画
     */
    private void expendAnimation() {
        if (mIsStopAnimator) {
            return;
        }
        // 左边跑
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView, TRANSLATION_X, 0, -mTranslationDistance);
        // 右边跑
        ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView, TRANSLATION_X, 0, mTranslationDistance);

        // 弹性效果  荡秋千一样 差值器   刚开始快越来越慢

        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIMATION_TIME);
        set.playTogether(leftTranslationAnimator, rightTranslationAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 往里面跑
                innerAnimation();
            }
        });
        set.start();
    }

    private void innerAnimation() {
        if (mIsStopAnimator) {
            return;
        }
        // 左边跑
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView, TRANSLATION_X, -mTranslationDistance, 0);
        // 右边跑
        ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView, TRANSLATION_X, mTranslationDistance, 0);
        AnimatorSet set = new AnimatorSet();
        set.setInterpolator(new AccelerateInterpolator());
        set.setDuration(ANIMATION_TIME);
        set.playTogether(leftTranslationAnimator, rightTranslationAnimator);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 往里外面跑
                // 切换颜色顺序  左边的给中间 中间的给右边  右边的给左边
                int leftColor = mLeftView.getColor();
                int rightColor = mRightView.getColor();
                int middleColor = mMiddleView.getColor();
                mMiddleView.exchangeColor(leftColor);
                mRightView.exchangeColor(middleColor);
                mLeftView.exchangeColor(rightColor);
                expendAnimation();
            }
        });
        set.start();
    }

    public void show() {
        this.setVisibility(VISIBLE);
    }


    protected void dismiss() {
        // 清理动画
        mLeftView.clearAnimation();
        mRightView.clearAnimation();
        clearAnimation();
        mIsStopAnimator = true;
        this.setVisibility(GONE);
    }

    private DotView getDotView(Context context) {
        DotView circleView = new DotView(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(PxUtils.dp2px(10), PxUtils.dp2px(10));
        params.addRule(CENTER_IN_PARENT);
        circleView.setLayoutParams(params);
        return circleView;
    }
}
